home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / util / cli / LibMon.lha / Src / flush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-28  |  3.7 KB  |  142 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/libraries.h>
  5. #include <exec/execbase.h>
  6. #include <dos/dos.h>
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9.  
  10. #include <stdio.h>
  11.  
  12. #include "global.h"
  13. #include "libinfo.h"
  14. #include "output.h"
  15. #include "flush.h"
  16.  
  17. extern struct ExecBase *SysBase;
  18.  
  19. /*
  20. ** Flush specified library
  21. */
  22. void FlushLib(STRPTR LibName)
  23. {
  24.   struct Library *Library;
  25.   struct LibInfo *LibInfoItem = NULL;
  26.   
  27.   Forbid();  /* Keep the system to ourself */
  28.   
  29.   Library = (struct Library *)FindName(&SysBase->LibList,LibName);
  30.   if (Library)
  31.   {
  32.     /* OK, we found the library, now let's try to remove it... */
  33.     LibInfoItem = AddLibInfoItem(NULL,Library);  /* Save inportant info... */
  34.     if (LibInfoItem)
  35.     {
  36.       /* Only attempt to flush a library if it isn't being used */
  37.       if (LibInfoItem->OpenCount == 0)
  38.       {
  39.         RemLibrary(Library);
  40.         
  41.         /* Here's an idea - let's search the LibList again.  If the */
  42.         /* Library isn't there, we must have flushed it             */
  43.         if (!FindName(&SysBase->LibList,LibName))
  44.           /* Yay, we got rid of the library! */
  45.           LibInfoItem->Flushed = TRUE;
  46.       }
  47.     }
  48.   }
  49.   
  50.   Permit();  /* OK, back to the ol' multitasking */
  51.   
  52.   if (Library)
  53.   {
  54.     /* The library was present, now we'll see if we flushed it */
  55.     if (LibInfoItem)
  56.     {
  57.       if (LibInfoItem->Flushed)
  58.         OutputFlushedLibInfo(LibInfoItem);
  59.     
  60.       FreeVec((APTR)LibInfoItem);  /* Don't need this memory any more */
  61.     }
  62.     else
  63.     {
  64.       /* Well, we run out of memory. So we're outa here! */
  65.       fputs("Error: Out of memory!\n",stderr);
  66.       clean_exit(20);
  67.     }
  68.   }
  69.   else
  70.     /* The library wasn't found in memory */
  71.     OutputLibNotFound(LibName);
  72. }
  73.  
  74.   
  75. /*
  76. ** Flush all libraries from memory
  77. */
  78. void FlushLibs(void)
  79. {
  80.   struct List *LibInfoList;
  81.   struct LibInfo *LibInfoItem;
  82.   ULONG FlushCount;
  83.   
  84.   Forbid();  /* Can't have anyone messing up the Library list while we are */
  85.   
  86.   LibInfoList = BuildLibInfoList();
  87.   if (LibInfoList)
  88.   {
  89.     /* Some libraries open others, so we gotta go round and round until */
  90.     /* we get them all                                                  */
  91.     do
  92.     {
  93.       FlushCount = 0;
  94.       LibInfoItem = (struct LibInfo *)LibInfoList->lh_Head;
  95.       while (LibInfoItem->li_Node.ln_Succ)
  96.       {
  97.         /* Don't try to flush anything that's already been flushed */
  98.         /* or is still open ...                                    */
  99.         if ((LibInfoItem->Flushed == FALSE) &&
  100.             (LibInfoItem->OpenCount == 0))
  101.         {
  102.           /* Attemp to flush the library... */
  103.           RemLibrary((struct Library *)LibInfoItem->Address);
  104.           
  105.           /* Check that the library is really gone... */
  106.           if (!FindName(&SysBase->LibList,LibInfoItem->Name))
  107.           {
  108.             LibInfoItem->Flushed = TRUE;
  109.             FlushCount++;
  110.           }
  111.         }
  112.         
  113.         /* Move Along */
  114.         LibInfoItem = (struct LibInfo *)LibInfoItem->li_Node.ln_Succ;
  115.       }
  116.     } while (FlushCount != 0);
  117.     /* Flush count will only be zero if no libraries were flushed, */
  118.     /* hence no more libraries need to be flushed                  */
  119.   }
  120.   
  121.   Permit();
  122.   
  123.   if (!LibInfoList)
  124.   {
  125.     /* Outta memory */
  126.     fputs("Error: Out of memory!\n",stderr);
  127.     clean_exit(20);
  128.   }
  129.   
  130.   /* Goody, now we have a list of libraries with flushing info */
  131.   LibInfoItem = (struct LibInfo *)LibInfoList->lh_Head;
  132.   while (LibInfoItem->li_Node.ln_Succ)
  133.   {
  134.     if (LibInfoItem->Flushed)
  135.       OutputFlushedLibInfo(LibInfoItem);
  136.     
  137.     LibInfoItem = (struct LibInfo *)LibInfoItem->li_Node.ln_Succ;
  138.   }
  139.   
  140.   KillLibInfoList(LibInfoList);
  141. }
  142.